Spot-On
spot-on-listener.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_listener_h_
29 #define _spoton_listener_h_
30 
31 #include <QHash>
32 #include <QPointer>
33 #include <QSqlDatabase>
34 #include <QTcpServer>
35 #include <QTimer>
36 #if QT_VERSION >= 0x050300 && defined(SPOTON_WEBSOCKETS_ENABLED)
37 #include <QWebSocketServer>
38 #endif
39 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
40 #include <qbluetoothserver.h>
41 #include <qbluetoothserviceinfo.h>
42 #include <qbluetoothsocket.h>
43 #endif
44 
45 #include "spot-on-neighbor.h"
46 
47 class QNetworkInterface;
49 class spoton_sctp_server;
50 
51 class spoton_listener_tcp_server: public QTcpServer
52 {
53  Q_OBJECT
54 
55  public:
56  spoton_listener_tcp_server(const qint64 id, QObject *parent):
57  QTcpServer(parent)
58  {
59  m_id = id;
60  }
61 
63  {
64  }
65 
66  void setMaxPendingConnections(const int maxPendingConnections)
67  {
68  QTcpServer::setMaxPendingConnections(qMax(1, maxPendingConnections));
69  }
70 
71  void incomingConnection(qintptr socketDescriptor);
72 
73  private:
74  qint64 m_id;
75 
76  signals:
77  void newConnection(const qintptr socketDescriptor,
78  const QHostAddress &address,
79  const quint16 port);
80 };
81 
82 class spoton_listener_udp_server: public QUdpSocket
83 {
84  Q_OBJECT
85 
86  public:
87  spoton_listener_udp_server(const qint64 id, QObject *parent):
88  QUdpSocket(parent)
89  {
90  m_id = id;
91  m_maxPendingConnections = 5;
92  connect(this,
93  SIGNAL(readyRead(void)),
94  this,
95  SLOT(slotReadyRead(void)));
96  }
97 
99  {
100  }
101 
102  bool clientExists(const QHostAddress &address, const quint16 port) const
103  {
104  return m_clients.contains(QString("%1:%2:%3").
105  arg(address.toString()).
106  arg(address.scopeId()).
107  arg(port));
108  }
109 
110  int maxPendingConnections(void) const
111  {
112  return m_maxPendingConnections;
113  }
114 
115  void addClientAddress(const QString &address)
116  {
117  m_clients.insert(address, 0);
118  }
119 
120  void setMaxPendingConnections(const int maxPendingConnections)
121  {
122  m_maxPendingConnections = qMax(1, maxPendingConnections);
123  }
124 
125  private:
126  QHash<QString, char> m_clients;
127  int m_maxPendingConnections;
128  qint64 m_id;
129 
130  private slots:
131  void slotClientDestroyed(QObject *object)
132  {
133  if(!object)
134  return;
135 
136  QString client(object->property("address").toString());
137 
138  m_clients.remove(client);
139  }
140 
141  void slotReadyRead(void);
142 
143  signals:
144  void newConnection(const qintptr socketDescriptor,
145  const QHostAddress &address,
146  const quint16 port);
147  void newDatagram(const QByteArray &datagram,
148  const QHostAddress &address,
149  const quint16 port);
150 };
151 
152 #if QT_VERSION >= 0x050300 && defined(SPOTON_WEBSOCKETS_ENABLED)
153 class spoton_listener_websocket_server: public QWebSocketServer
154 {
155  public:
156  spoton_listener_websocket_server(const QWebSocketServer::SslMode sslMode,
157  const qint64 id,
158  QObject *parent):
159  QWebSocketServer(QString::number(id), sslMode, parent)
160  {
161  m_id = id;
162  }
163 
164  ~spoton_listener_websocket_server()
165  {
166  }
167 
168  void setMaxPendingConnections(const int maxPendingConnections)
169  {
170  QWebSocketServer::setMaxPendingConnections(qMax(1, maxPendingConnections));
171  }
172 
173  private:
174  qint64 m_id;
175 };
176 #endif
177 
178 class spoton_listener: public QObject
179 {
180  Q_OBJECT
181 
182  public:
183  spoton_listener(const QString &ipAddress,
184  const QString &port,
185  const QString &scopeId,
186  const int maximumClients,
187  const qint64 id,
188  const QString &echoMode,
189  const unsigned int keySize,
190  const QByteArray &certificate,
191  const QByteArray &privateKey,
192  const QByteArray &publicKey,
193  const bool useAccounts,
194  const qint64 maximumBufferSize,
195  const qint64 maximumContentLength,
196  const QString &transport,
197  const bool shareAddress,
198  const QString &orientation,
199  const QString &motd,
200  const QString &sslControlString,
201  const int laneWidth,
202  const int passthrough,
203  const int sourceOfRandomness,
204  const QByteArray &privateApplicationCredentials,
205  const QString &socketOptions,
206  QObject *parent);
207  ~spoton_listener();
208  QHostAddress externalAddress(void) const;
209  QHostAddress localAddress(void) const;
210  QString orientation(void) const;
211  QString serverAddress(void) const;
212  QString transport(void) const;
213  bool isListening(void) const;
214  bool listen(const QString &address, const quint16 port);
215  quint16 externalPort(void) const;
216  quint16 serverPort(void) const;
217  void close(void);
218  void updateConnectionCount(void);
219 
220  private:
221 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
222  QBluetoothServiceInfo *m_bluetoothServiceInfo;
223 #endif
224  QByteArray m_certificate;
225  QByteArray m_privateApplicationCredentials;
226  QByteArray m_privateKey;
227  QByteArray m_publicKey;
228  QNetworkInterface *m_networkInterface;
229 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
230  QPointer<QBluetoothServer> m_bluetoothServer;
231 #endif
232  QPointer<spoton_external_address> m_externalAddress;
233  QPointer<spoton_listener_tcp_server> m_tcpServer;
234  QPointer<spoton_listener_udp_server> m_udpServer;
235 #if QT_VERSION >= 0x050300 && defined(SPOTON_WEBSOCKETS_ENABLED)
236  QPointer<spoton_listener_websocket_server> m_webSocketServer;
237 #endif
238  QPointer<spoton_sctp_server> m_sctpServer;
239  QString m_address;
240  QString m_echoMode;
241  QString m_motd;
242  QString m_orientation;
243  QString m_scopeId;
244  QString m_socketOptions;
245  QString m_sslControlString;
246  QString m_transport;
247  QTimer m_externalAddressDiscovererTimer;
248  QTimer m_timer;
249  bool m_shareAddress;
250  bool m_useAccounts;
251  int m_laneWidth;
252  int m_maximumClients;
253  int m_passthrough;
254  int m_sourceOfRandomness;
255  qint64 m_id;
256  qint64 m_maximumBufferSize;
257  qint64 m_maximumContentLength;
258  quint16 m_externalPort;
259  quint16 m_port;
260  unsigned int m_keySize;
261  QString errorString(void) const;
262  int maxPendingConnections(void) const;
263  qint64 id(void) const;
264  void prepareNetworkInterface(void);
265  void saveExternalAddress(const QHostAddress &address,
266  const QSqlDatabase &db);
267  void saveStatus(const QSqlDatabase &db);
268 
269  private slots:
270  void slotDiscoverExternalAddress(void);
271  void slotExternalAddressDiscovered(const QHostAddress &address);
272  void slotNeighborDisconnected(void);
273  void slotNewConnection(const qintptr socketDescriptor,
274  const QHostAddress &address,
275  const quint16 port);
276 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
277  void slotNewConnection(void);
278 #endif
279 #if QT_VERSION >= 0x050300 && defined(SPOTON_WEBSOCKETS_ENABLED)
280  void slotNewWebSocketConnection(void);
281 #endif
282  void slotTimeout(void);
283 
284  signals:
285  void newNeighbor(const QPointer<spoton_neighbor> &neighbor);
286 };
287 
288 #endif
spoton_listener
Definition: spot-on-listener.h:178
spoton_sctp_server
Definition: spot-on-sctp-server.h:39
spoton_listener_udp_server
Definition: spot-on-listener.h:82
spoton_listener_tcp_server
Definition: spot-on-listener.h:51
spoton_external_address
Definition: spot-on-external-address.h:35